package u13.ejercicio18;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Ejercicio18 extends JFrame implements MouseListener, ActionListener, ChangeListener{
	
	private int anchoVentana = 400;
	private int altoVentana = 400;
	private JPanel p;
	private JButton bCuadrado;
	private JButton bCirculo;
	private JButton bBorrar;
	private MiPanelDibujo pDibujo;
	private JLabel lPosX;
	private JLabel lPosY;
	private JCheckBox ckCirculos;
	private JCheckBox ckCuadrados;
	
	public Ejercicio18(){
		inicializarFrame();
		inicializarLayout();
		setVisible(true);
	}
	
	private void inicializarLayout() {
		//creamos el panel principal sobre el que va el panelDibujo y panelMenu
		this.p=new JPanel();
		p.setLayout(new BorderLayout());
		pDibujo = new MiPanelDibujo();
		pDibujo.setBorder(new LineBorder(Color.ORANGE));
		pDibujo.addMouseListener(this);
		p.add(pDibujo,BorderLayout.CENTER);
		
		JPanel panelMenu=new JPanel();
		panelMenu.setLayout(new GridLayout(7,1));
		panelMenu.setBorder(new LineBorder(Color.BLUE));
		
			bCuadrado = new JButton("Cuadrado");bCuadrado.addActionListener(this);bCuadrado.setActionCommand("Cuadrado"); panelMenu.add(bCuadrado);
			bCirculo = new JButton("Circulo");bCirculo.addActionListener(this);bCirculo.setActionCommand("Circulo");panelMenu.add(bCirculo);
			bBorrar = new JButton("Borrar");bBorrar.addActionListener(this);bBorrar.setActionCommand("Borrar");panelMenu.add(bBorrar);
		
			JPanel pPosX=new JPanel();pPosX.setLayout(new GridLayout(1,2));
				JLabel lX=new JLabel("X: ");pPosX.add(lX);
				lPosX = new JLabel();pPosX.add(lPosX);
				pPosX.setBorder(new LineBorder(Color.BLUE));
			panelMenu.add(pPosX);
		
			JPanel pPosY=new JPanel();pPosY.setLayout(new GridLayout(1,2));
				JLabel lY=new JLabel("Y: ");pPosY.add(lY);
				lPosY = new JLabel();pPosY.add(lPosY);
				pPosY.setBorder(new LineBorder(Color.BLUE));
			panelMenu.add(pPosY);
			//creamos los Checkbox
			ckCirculos = new JCheckBox("Ver Circulos", true);
			ckCirculos.addChangeListener(this);
			panelMenu.add(ckCirculos);
			
			ckCuadrados = new JCheckBox("Ver Cuadrados", true);
			ckCuadrados.addChangeListener(this);
			panelMenu.add(ckCuadrados);
			
		p.add(panelMenu,BorderLayout.EAST);
		
		add(p);
	}
	
	private void inicializarFrame() {
		
		int anchoPantalla = Toolkit.getDefaultToolkit().getScreenSize().width;
		int altoPantalla = Toolkit.getDefaultToolkit().getScreenSize().height;
		setSize(anchoVentana, altoVentana);
		setLocation(anchoPantalla / 2 - anchoVentana / 2, altoPantalla / 2
				- altoVentana / 2);
		setTitle("Ejercicio 18");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new GridLayout(1,1));
		
	}
	
	public static void main(String[] args) {
		new Ejercicio18();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		lPosX.setText(Integer.toString(e.getX()));
		lPosY.setText(Integer.toString(e.getY()));
	}

	

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand().equals("Cuadrado")){
			pDibujo.getFiguras().add(new Cuadrado(Integer.parseInt(lPosX.getText()),Integer.parseInt(lPosY.getText())));
			//pDibujo.repaint();
		}
		else if(e.getActionCommand().equals("Circulo")){
			pDibujo.getFiguras().add(new Circulo(Integer.parseInt(lPosX.getText()),Integer.parseInt(lPosY.getText())));
			//pDibujo.repaint();
		}
		else if(e.getActionCommand().equals("Borrar")){
			pDibujo.getFiguras().clear();
		}
		
		pDibujo.repaint();
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		//Esto se hace asi porque los checkbox no tienen componentes de Action
		
	}
	
}
